﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.ChamCongDelegate;
using System.Text.RegularExpressions;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.ReportsModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.TinhLuong;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.TinhLuong;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.ReportsDelegate
{
    public class ReportsServiceDelegate
    {
        #region Private Member

        const string _tenDoanhNghiep = "DNTT TMDV LỘC BÌNH AN";
        CommonDelegate.CommonDelegate _commonDelegate = new CommonDelegate.CommonDelegate();
        ChamCongServiceDelegate _chamCongServiceDelegate = new ChamCongServiceDelegate();
        TinhLuongHangThangServiceDelegate _tinhLuongHangThangServiceDelegate = new TinhLuongHangThangServiceDelegate();

        #endregion

        #region Hỗ trợ Xuất Report

        /// <summary>
        /// Xuất file .doc từ chuỗi html (Sử dụng font Times New Roman)
        /// </summary>
        /// <param name="BodyContent">Nội dung Html (Không chứa thẻ [html] và [body])</param>
        /// <param name="FileName">Tên file cần lưu</param>
        public void ExportHtmlToWord(string BodyContent, string FileName)
        {
            string HtmlContent = AddBodyTagForExportWord(BodyContent);

            HttpContext.Current.Response.Clear();   //' Clear Response and set content type and disposition so that user get save file dialogue. 
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", FileName));
            HttpContext.Current.Response.ContentType = "application/doc";
            HttpContext.Current.Response.Write(HtmlContent);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// Xuất file .xls từ chuỗi html (Sử dụng font Times New Roman)
        /// </summary>
        /// <param name="BodyContent">Nội dung Html (Không chứa thẻ [html] và [body])</param>
        /// <param name="FileName">Tên file cần lưu</param>
        public void ExportHtmlToExcel(string BodyContent, string FileName)
        {

            HttpContext.Current.Response.Clear();   //' Clear Response and set content type and disposition so that user get save file dialogue. 
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", FileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write(BodyContent);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// Thêm tab html và body cho chuỗi html khi xuất file word
        /// </summary>
        /// <param name="BodyContent"></param>
        /// <returns></returns>
        public string AddBodyTagForExportWord(string BodyContent)
        {
            string result = string.Empty;

            result = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word'>\n";
            result += "<head>\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n";
            result += @"<!--[if gte mso 9]>
                             <xml>
                             <w:WordDocument>
                             <w:View>Print</w:View>
                             <w:Zoom>90</w:Zoom>
                             <w:DoNotOptimizeForBrowser/>
                             </w:WordDocument>
                             </xml>
                             <![endif]-->";
            result += "\n<style type='text/css'>p{}</style>\n";
            result += "</head>\n";
            result += "<body encoding='Identity-H' style='font-family: Times New Roman;'>\n";
            result += BodyContent;
            result += "\n</body>";
            result += "\n<html>";

            return result;
        }

        /// <summary>
        /// Thêm tab html và body cho chuỗi html khi xuất file pdf sử dụng iTextsharp
        /// </summary>
        /// <param name="BodyContent"></param>
        /// <returns></returns>
        public string AddBodyTagForExportPdfiTextsharp(string BodyContent, bool ChangImageUrl)
        {
            string result = string.Empty;

            result = "<html>";
            result += "<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></head>";
            //result += "<body encoding='" + BaseFont.IDENTITY_H + "' style='font-family: Times New Roman;'>";
            if (ChangImageUrl)
            {
                result += this.ChangeImageUrl(BodyContent);
            }
            else
            {
                result += BodyContent;
            }
            result += "</body>";
            result += "<html>";

            return result;
        }

        private string ChangeImageUrl(string input)
        {
            if (input == null)
                return string.Empty;
            string tempInput = input;
            string patternImg = @"<img(.|\n)+?>";
            string src = string.Empty;
            HttpContext context = HttpContext.Current;

            //Change the relative URL's to absolute URL's for an image, if any in the HTML code.
            foreach (Match m in System.Text.RegularExpressions.Regex.Matches(input, patternImg, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.RightToLeft))
            {
                if (m.Success)
                {
                    string tempM = m.Value;
                    string patternSrc = "src=[\'|\"](.+?)[\'|\"]";
                    Regex reImg = new Regex(patternSrc, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    Match mImg = reImg.Match(m.Value);

                    if (mImg.Success)
                    {
                        src = mImg.Value.ToLower().Replace("src=", "").Replace("\"", "");

                        if (src.ToLower().Contains("http://") == false)
                        {
                            //Insert new URL in img tag
                            //src = "src=\"" + context.Request.Url.Scheme + "://" +
                            //    context.Request.Url.Authority + src + "\"";
                            src = "src=\"" + context.Server.MapPath("~/" + src) + "\"";
                            //src = "src=\"" + context.Request.Url.ToString() + "\"";
                            try
                            {
                                tempM = tempM.Remove(mImg.Index, mImg.Length);
                                tempM = tempM.Insert(mImg.Index, src);

                                //insert new url img tag in whole html code
                                tempInput = tempInput.Remove(m.Index, m.Length);
                                tempInput = tempInput.Insert(m.Index, tempM);
                            }
                            catch (Exception e)
                            {

                            }
                        }
                    }
                }
            }
            return tempInput;
        }

        #endregion

        #region BACNP

        public IList<BangLuongNhanVienForm> BuildListBangLuongNhanVienForm(int idBangLuong)
        {
            var result = new List<BangLuongNhanVienForm>();

            var listBangTinhLuong = _tinhLuongHangThangServiceDelegate.ReportGetAllTinhLuongByIdBangTinhLuong(idBangLuong);

            foreach (var item in listBangTinhLuong)
            {
                result.Add(BuildBangLuongNhanVienForm(item));
            }

            #region row1
            //var row1 = new BangLuongNhanVienForm();

            //row1.SoThe = "11001";
            //row1.Ho = "Võ Ngọc";
            //row1.Ten = "Như";
            //row1.SoTienNguoiPhuThuoc = "4,000,000";
            //row1.LuongCanBanHopDong = "2,150,000";
            //row1.SoNgayCong = "25";
            //row1.LuongCanBanThucLanh = "2,067,308";

            //row1.PCTrachNhiem = "500,000";
            //row1.SoNgayHuongPCTrachNhiem = "21";
            //row1.PCTrachNhiemThucLanh = "403,846";

            //row1.TCChuyenCan = "100,000";
            //row1.SoNgayHuongTCChuyenCan = "21";
            //row1.TCChuyenCanThucLanh = "80,769";

            //row1.PhuCapDiLai = "200,000";
            //row1.SoNgayHuongPhuCapDiLai = "21";
            //row1.PhuCapDiLaiThucLanh = "161,538";

            //row1.PhuCapTienCom = "200,000";
            //row1.SoNgayHuongPhuCapTienCom = "21";
            //row1.PhuCapTienComThucLanh = "161,538";

            //row1.PhuCapDienThoai = "100,000";
            //row1.SoNgayHuongPhuCapDienThoai = "21";
            //row1.PhuCapDienThoaiThucLanh = "80,769";

            //row1.NghiLeNghiPhepCheDo = "1";
            //row1.TienNghiLeNghiPhepCheDo = "82,692";
            //row1.SoTienThuongKmThucLanh = "0";
            //row1.SoTienThuongChuyenThucLanh = "0";

            //#region Vùng không được hiển thị
            //////////////////////////////////////////////////////////
            ////Tổng tăng ca 100%
            //row1.TongTangCa100Percent = "";
            ////Trợ cấp tăng ca 50%
            //row1.TroCapTangCa50Percent = "";
            ////Trợ cấp tăng ca 100%
            //row1.TroCapTangCa100Percent = "";
            ////Trợ cấp ca đêm 30%
            //row1.TroCapCaDem30Percent = "";
            ////Trợ cấp ca đêm 45%
            //row1.TroCapCaDem45Percent = "";
            ////Nghỉ chế độ
            //row1.NghiCheDo = "";
            //////////////////////////////////////////////////////////
            //#endregion

            //row1.SoTienThuongHieuQuaCongViec = "";
            //row1.DieuChinh = "";
            //row1.BHXH = "150,500";
            //row1.BHYT = "32,250";
            //row1.BHTN = "";
            //row1.ThuNhapTruocThue = "-1,144,288";
            //row1.ThueThuNhap = "0";
            //row1.TongTienThucLanh = "2,855,712";
            //row1.TienTamUng = "";
            //row1.TienABC = "2,855,712";
            //row1.TienTraGop = "3,038,462";
            //row1.TongTienKhauTru = "182,750";

            //result.Add(row1);
            #endregion

            #region row2
            //var row2 = new BangLuongNhanVienForm();
            //row2.SoThe = "11001";
            //row2.Ho = "Võ Ngọc";
            //row2.Ten = "Như";
            //row2.SoTienNguoiPhuThuoc = "4,000,000";
            //row2.LuongCanBanHopDong = "2,150,000";
            //row2.SoNgayCong = "25";
            //row2.LuongCanBanThucLanh = "2,067,308";

            //row2.PCTrachNhiem = "500,000";
            //row2.SoNgayHuongPCTrachNhiem = "21";
            //row2.PCTrachNhiemThucLanh = "403,846";

            //row2.TCChuyenCan = "100,000";
            //row2.SoNgayHuongTCChuyenCan = "21";
            //row2.TCChuyenCanThucLanh = "80,769";

            //row2.PhuCapDiLai = "200,000";
            //row2.SoNgayHuongPhuCapDiLai = "21";
            //row2.PhuCapDiLaiThucLanh = "161,538";

            //row2.PhuCapTienCom = "200,000";
            //row2.SoNgayHuongPhuCapTienCom = "21";
            //row2.PhuCapTienComThucLanh = "161,538";

            //row2.PhuCapDienThoai = "100,000";
            //row2.SoNgayHuongPhuCapDienThoai = "21";
            //row2.PhuCapDienThoaiThucLanh = "80,769";

            //row2.NghiLeNghiPhepCheDo = "1";
            //row2.TienNghiLeNghiPhepCheDo = "82,692";
            //row2.SoTienThuongKmThucLanh = "0";
            //row2.SoTienThuongChuyenThucLanh = "0";

            //#region Vùng không được hiển thị
            //////////////////////////////////////////////////////////
            ////Tổng tăng ca 100%
            //row2.TongTangCa100Percent = "";
            ////Trợ cấp tăng ca 50%
            //row2.TroCapTangCa50Percent = "";
            ////Trợ cấp tăng ca 100%
            //row2.TroCapTangCa100Percent = "";
            ////Trợ cấp ca đêm 30%
            //row2.TroCapCaDem30Percent = "";
            ////Trợ cấp ca đêm 45%
            //row2.TroCapCaDem45Percent = "";
            ////Nghỉ chế độ
            //row2.NghiCheDo = "";
            //////////////////////////////////////////////////////////
            //#endregion

            //row2.SoTienThuongHieuQuaCongViec = "";
            //row2.DieuChinh = "";
            //row2.BHXH = "150,500";
            //row2.BHYT = "32,250";
            //row2.BHTN = "";
            //row2.ThuNhapTruocThue = "-1,144,288";
            //row2.ThueThuNhap = "0";
            //row2.TongTienThucLanh = "2,855,712";
            //row2.TienTamUng = "";
            //row2.TienABC = "2,855,712";
            //row2.TienTraGop = "3,038,462";
            //row2.TongTienKhauTru = "182,750";

            //result.Add(row2);
            #endregion

            return result;
        }

        private BangLuongNhanVienForm BuildBangLuongNhanVienForm(tb_TinhLuongThangChiTiet objTinhLuongThangChiTiet)
        {
            BangLuongNhanVienForm form = new BangLuongNhanVienForm();

            var objNhanVien = _chamCongServiceDelegate.GetNhanVienById(objTinhLuongThangChiTiet.MaNhanVien);

            form.SoThe = objNhanVien.SoThe;
            form.Ho = objNhanVien.Ho;
            form.Ten = objNhanVien.Ten;

            form.SoNgayCong = objTinhLuongThangChiTiet.NgayCongLamViec != null ? objTinhLuongThangChiTiet.NgayCongLamViec.Value.ToString("#,0.#") : "0";
            form.SoTienNguoiPhuThuoc = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.SoTienNguoiPhuThuoc);
            form.LuongCanBanHopDong = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.LuongCanBan);
            form.LuongCanBanThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.LuongCongNhat);
            //Phụ cấp trách nhiệm
            form.PCTrachNhiem = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhucCapTrachNhiem);
            form.SoNgayHuongPCTrachNhiem = form.SoNgayCong;
            form.PCTrachNhiemThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PCTrachNhiemCongNhat);
            //Trợ cấp chuyên cần
            form.TCChuyenCan = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.ChuyenCan);
            form.SoNgayHuongTCChuyenCan = form.SoNgayCong;
            form.TCChuyenCanThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.ChuyenCanCongNhat);
            //Phụ cấp đi lại
            form.PhuCapDiLai = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapDiLai);
            form.SoNgayHuongPhuCapDiLai = form.SoNgayCong;
            form.PhuCapDiLaiThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapDiLaiCongNhat);
            //Phụ cấp tiền cơm
            form.PhuCapTienCom = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapTienCom);
            form.SoNgayHuongPhuCapTienCom = form.SoNgayCong;
            form.PhuCapTienComThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapTienComCongNhat);
            //Phụ cấp điện thoại
            form.PhuCapDienThoai = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapDienThoai);
            form.SoNgayHuongPhuCapDienThoai = form.SoNgayCong;
            form.PhuCapDienThoaiThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.PhuCapDienThoaiCongNhat);
            //Số ngày nghỉ lễ, phép năm, chế độ
            form.NghiLeNghiPhepCheDo = objTinhLuongThangChiTiet.NghiLeNghiPhepCheDo != null ? objTinhLuongThangChiTiet.NghiLeNghiPhepCheDo.Value.ToString("#,0.#") : "0";
            //Số tiền nghỉ lễ, phép năm, chế độ được hưởng
            form.TienNghiLeNghiPhepCheDo = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienNghiLeNghiPhepCheDo);
            //Tiền thưởng KM
            form.SoTienThuongKmThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.SoTienThuongKmCongNhat);
            //Tiền thưởng chuyến    (C)
            form.SoTienThuongChuyenThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.SoTienThuongChuyenCongNhat);

            ////////////////////////////////////////////////////////
            #region Vùng không được hiển thị
            ////////////////////////////////////////////////////////
            //Tổng tăng ca 100%
            form.TongTangCa100Percent = string.Empty;
            //Trợ cấp tăng ca 50%
            form.TroCapTangCa50Percent = string.Empty;
            //Trợ cấp tăng ca 100%
            form.TroCapTangCa100Percent = string.Empty;
            //Trợ cấp ca đêm 30%
            form.TroCapCaDem30Percent = string.Empty;
            //Trợ cấp ca đêm 45%
            form.TroCapCaDem45Percent = string.Empty;
            //Nghỉ chế độ
            form.NghiCheDo = string.Empty;
            ////////////////////////////////////////////////////////
            #endregion
            ////////////////////////////////////////////////////////

            //Tiền thưởng hiệu quả công việc
            form.SoTienThuongHieuQuaCongViec = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.SoTienThuongHieuQuaCongViec);
            //Tiền điều chỉnh
            form.DieuChinh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.DieuChinh);
            //Tiền bảo hiểm y tế
            form.BHYT = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienDongBaoHiemYTe);
            //Tiền bảo hiểm xã hội
            form.BHXH = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienDongBaoHiemXaHoi);
            //Tiền bảo hiểm thất nghiệp
            form.BHTN = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienDongBaoHiemThatNghiep);
            //Tiền thu nhập trước thuế
            form.ThuNhapTruocThue = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienThuNhapTruocThue);
            //Tiền thuế thu nhập
            form.ThueThuNhap = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienThueThuNhap);
            //Tiền thực lãnh    (A)
            form.TongTienThucLanh = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienThucLanh);
            //Tiền tạm ứng      (B)
            form.TienTamUng = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienTamUng);
            //Tiền A - B - C
            form.TienABC = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TongTienNhanDuoc);
            //Tiền trả gộp
            form.TienTraGop = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TienTraGop);
            //Tổng tiền khấu trừ
            form.TongTienKhauTru = _commonDelegate.ConvertDecimalToMoneyString(objTinhLuongThangChiTiet.TongKhauTru);

            return form;
        }


        #endregion

        #region TAMNN
        #endregion

        #region TINHLT
        #endregion
    }
}